/**
 * Class: LogUtils
 *
 * Purpose: 
 *
 * @author Ken Scott
 */
package org.openfantasia.util;

import java.io.File;
import java.io.IOException;
import java.text.DateFormat;
import java.util.Date;

public class LogUtils {

	public static boolean LOG_FILE_ENABLED = false;

	public static boolean DEBUG_ENABLED = false;

	public static final DateFormat STD_DATE_TIME_MS_FORMAT = DateTime.STD_DATE_TIME_FORMAT;

	public static final DateFormat COMPACT_DATE_TIME_MS_FORMAT = DateTime.COMPACT_DATE_TIME_FORMAT;

	private static File LOGFILE = null;


	public static String getStdTime() {
		return STD_DATE_TIME_MS_FORMAT.format(new Date());
	}


	private static String getPrintPrefix() {
		StringBuilder sb = new StringBuilder();

		sb.append("[");
		sb.append(getStdTime());
		sb.append("] ");
		sb.append(Thread.currentThread().getName());

		return sb.toString();
	}


	public static void openLogFile(Class clz) {
		if (LOGFILE == null) {
			synchronized (Utils.class) {
				if (LOGFILE == null) {
					LOGFILE = new File(clz.getSimpleName() + "-" + COMPACT_DATE_TIME_MS_FORMAT.format(new Date()) + ".log");
					try {
						LOGFILE.createNewFile();
						System.out.println("NEW LOG FILE: " + LOGFILE.getName());
					}
					catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		else {
			System.out.println("LOG FILE ALREADY ACTIVE: " + LOGFILE.getName());
		}
	}


	private static File getLogFile() {
		if (LOGFILE == null) {
			synchronized (Utils.class) {
				if (LOGFILE == null) {
					LOGFILE = new File("LOG-" + COMPACT_DATE_TIME_MS_FORMAT.format(new Date()) + ".txt");
					try {
						LOGFILE.createNewFile();
					}
					catch (IOException e) {
						e.printStackTrace();
					}
				}
			}
		}
		return LOGFILE;
	}


	public static synchronized void log() {
		if (LOG_FILE_ENABLED) {
			FileUtils.appendTextToFile(getLogFile(), "\n");
		}
		System.out.println();
	}


	public static synchronized void log(int spaces, Object s) {
		String line = getPrintPrefix() + ": " + TextUtils.getSpaces(spaces) + s;
		if (LOG_FILE_ENABLED) {
			FileUtils.appendTextToFile(getLogFile(), line + "\n");
		}
		System.out.println(line);
	}


	public static synchronized void log(Object s) {
		String line = getPrintPrefix() + ": " + s;
		if (LOG_FILE_ENABLED) {
			FileUtils.appendTextToFile(getLogFile(), line + "\n");
		}
		System.out.println(line);
	}


	public static synchronized void error(Throwable t) {
		t.printStackTrace(System.out);
	}


	public static synchronized void error(Throwable t, Object s) {
		String line = getPrintPrefix() + ": " + s;
		if (LOG_FILE_ENABLED) {
			FileUtils.appendTextToFile(getLogFile(), line + "\n");
		}
		System.out.println(line);
		t.printStackTrace(System.out);
	}


	public static synchronized void lognlf(Object s) {
		if (LOG_FILE_ENABLED) {
			FileUtils.appendTextToFile(getLogFile(), s);
		}
		System.out.print(s);
	}


	public static boolean setDebugEnabled(boolean enabled) {
		boolean was = DEBUG_ENABLED;
		DEBUG_ENABLED = enabled;
		return was;
	}


	public static synchronized void debug(Object s) {
		if (DEBUG_ENABLED) {
			String line = getPrintPrefix() + " - DEBUG: " + s;
			if (LOG_FILE_ENABLED) {
				FileUtils.appendTextToFile(getLogFile(), line + "\n");
			}
			System.out.println(line);
		}
	}


	public static synchronized void debug(int spaces, Object s) {
		if (DEBUG_ENABLED) {
			String line = getPrintPrefix() + " - DEBUG: " + TextUtils.getSpaces(spaces) + s;
			if (LOG_FILE_ENABLED) {
				FileUtils.appendTextToFile(getLogFile(), line + "\n");
			}
			System.out.println(line);
		}
	}
}
